Comment.class_eval do
	def plugin
		Mephisto::Plugin[:comment_defensio]
	end
	  
  def check_approval(site, request)    
    self.approved = site.approve_comments?
    case which_comment_spam_filter?(site)
    when :defensio
      logger.info "Let's spool up Defensio"
      defensio = Defensio.new({:api_key => plugin.defensio_key, :blog => plugin.defensio_url})
      defensio_response = defensio.check_comment(comment_spam_options(:defensio, site, request))
      logger.debug defensio_response.to_yaml
      if defensio_response[:status] == 'success'        
        self.approved = !defensio_response[:spam]
        self.spam_signature = defensio_response[:signature]
        self.spam_spaminess = defensio_response[:spaminess]
        logger.info "Checking Defensio (#{plugin.defensio_key}) for new comment on Article #{article_id}.  #{approved? ? 'Approved' : 'Blocked'}"
      else
        self.approved = false
        logger.info "Defensio call failed (#{plugin.defensio_key}) for new comment on Article #{article_id}."
      end      
    when :akismet
      logger.info "Let's spool up akismet"
      akismet = Akismet.new(site.akismet_key, site.akismet_url)
      self.approved = !akismet.comment_check(comment_spam_options(:akismet, site, request))
      logger.info "Checking Akismet (#{site.akismet_key}) for new comment on Article #{article_id}.  #{approved? ? 'Approved' : 'Blocked'}"
      logger.warn "Odd Akismet Response: #{akismet.last_response.inspect}" unless Akismet.normal_responses.include?(akismet.last_response)
    when :none
      logger.info "No content spam filter defined"
    end
  end  
  
  def comment_spam_options(spam_engine, site, request)
    options = {
      :user_ip              => author_ip,
      :comment_author       => author,
      :comment_author_email => author_email, 
      :comment_author_url   => author_url,
      :permalink            => "http://#{request.host_with_port}#{site.permalink_for(self)}", 
      :comment_content      => body,
      :referrer             => referrer
    }
    case spam_engine      
    when :defensio
      options.merge( { :article_date => article.published_at.strftime("%Y/%m/%d"), :comment_type => 'comment'} )
    when :akismet
      options.merge( { :user_agent => user_agent} )
    end
  end  
  
  protected
    def which_comment_spam_filter?(site)      
      if [:defensio_key, :defensio_url].all? { |attr| !plugin.send(attr).blank? }
        logger.info('Which comment spam filter? Looks like Defensio is configured.')
        :defensio
      elsif [:akismet_key, :akismet_url].all? { |attr| !site.send(attr).blank? }      
        logger.info('Which comment spam filter? Looks like Akismet is configured.')
        :akismet
      else
        logger.info('Which comment spam filter? Looks like none is configured.')
        :none
      end    
    end

    def mark_comment(comment_type, site, request)
      case which_comment_spam_filter?(site)
      when :defensio
        response = Defensio.new({:api_key => plugin.defensio_key, :blog => plugin.defensio_url}).send("mark_as_#{comment_type.to_s}", {:signatures => spam_signature})
        logger.info "Calling Defensio (#{plugin.defensio_key}) for #{comment_type} comment on Article #{article_id}.  #{response}"
      when :akismet
        response = Akismet.new(site.akismet_key, site.akismet_url).send("submit_#{comment_type}", comment_spam_options(:akismet, site, request))
        logger.info "Calling Akismet (#{site.akismet_key}) for #{comment_type} comment on Article #{article_id}.  #{response}"
      end
    end    
end